package JavaDowork.pratice6;
import java.util.Scanner;

public class Q2 {

    public static void main(String[] args) {
        // 定义一个长度为10的double类型数组
        double[] numbers = new double[10];

        // 从控制台为数组赋值
        for (int i = 0; i < numbers.length; i++) {
            System.out.print("请输入第" + (i + 1) + "个元素的值：");
            numbers[i] = new Scanner(System.in).nextDouble();
        }

        // 调用printArr()方法输出数组元素
        printArr(numbers);

        // 调用getSum()方法计算并输出数组元素的累加和
        double sum = getSum(numbers);
        System.out.println("数组元素的累加和为：" + sum);
    }

    // 遍历并输出数组元素的方法
    static void printArr(double[] numbers) {
        System.out.println("数组元素如下：");
        for (double number : numbers) {
            System.out.print(number + " ");
        }
        System.out.println();
    }

    // 计算并返回数组元素累加和的方法
    static double getSum(double[] numbers) {
        double sum = 0;
        for (double number : numbers) {
            sum += number;
        }
        return sum;
    }
}
